import 'package:flutter/material.dart';

import 'package:polyv_video_plugin/polyv_live_video_plugin.dart';
import 'package:polyv_video_plugin/polyv_video_plugin.dart';
import 'package:polyv_video_plugin/polyv_utils.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // PolyvLiveController _liveVideoController;

 PolyvController _liveVideoController;

  double progressValue = 0.5;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    PolyvUtils.playerConfig(
      config:
          "LfN9Td+rVEflu4/wAePp8HlQbRG3h5PiwBhO0S9wHRk2hfqEL3RAo/bDtFBJUAhid7OuJkVoZ8gKQks9sMEFhU4A/eSIufkH13e2Hzy/3Rtzs6JnVNjSknRqGNlbZV8MYveM5xXkKTDJLJxk2vfL1A==",
      aesKey: "VXtlHmwfS2oYm0CZ",
      iv: "2u9gDPKdX6GyQJKU",
      appId: "endz3vf26q",
      appSecret: "ba95e2f2fd394ff19a1cd1b248839102",
    );
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
             Container(
               height: 212,
               child: PolyvPlayer(
                   onCreated: onViewPlayerCreated,
                ),
             ),
            // Container(
            //   height: 240,
            //   child: PolyvLivePlayer(
            //     onCreated: onViewPlayerCreated,
            //   ),
            // ),
            // GestureDetector(
            //   child: Text("设置直播播放"),
            //   onTap: () {
            //     _liveVideoController.setLivePlay(
            //         userId: "e77rl86axa", channelId: "127889");
            //   },
            // ),
            // GestureDetector(
            //   child: Text("初始化弹幕"),
            //   onTap: () {
            //     _liveVideoController.initDanMu(
            //         padding: 10, textColor: "#ff0000", textSize: 20);
            //   },
            // ),
            // GestureDetector(
            //   child: Text("发送弹幕"),
            //   onTap: () {
            //     _liveVideoController.sendDanMu("我来了，快来看");
            //   },
            // ),
            // GestureDetector(
            //   child: Text("显示弹幕"),
            //   onTap: () {
            //     _liveVideoController.showDanMu();
            //   },
            // ),
            // GestureDetector(
            //   child: Text("隐藏弹幕"),
            //   onTap: () {
            //     _liveVideoController.hideDanMu();
            //   },
            // ),
            GestureDetector(
              child: Text("暂停"),
              onTap: () {
                print("Text 点击事件");
                _liveVideoController.pause();
              },
            ),
            GestureDetector(
              child: Text("继续"),
              onTap: () {
                print("Text 点击事件");
                _liveVideoController.resume();
//                AutoOrientation.portraitAutoMode();
              },
            ),
            // GestureDetector(
            //   child: Text("测试按钮"),
            //   onTap: () async {
            //     var result = await _liveVideoController.getChatHistory(
            //         channelId: "233", end: "20", begin: "0", userId: "333");
            //     print(result);

            //     //                AutoOrientation.portraitAutoMode();
            //   },
            // ),
            GestureDetector(
              child: Text("获取封面图片"),
              onTap: () async {
                print(await PolyvUtils.getFirstImage(
                    vid: "ef4825bc7e585a9b7b5894ded7ded188_e"));
                //                AutoOrientation.portraitAutoMode();
              },
            ),

//             Container(
//               child: new Slider(
//                 value: progressValue, //实际进度的位置
//                 inactiveColor: Colors.white, //进度中不活动部分的颜色
//                 label: '$progressValue',
//                 min: 0.0,
//                 max: 100.0,
//                 divisions: 1000,
//                 activeColor: Colors.green[600], //进度中活动部分的颜色
//                 onChangeStart: (double) {
//                   print('888888888888onChangeStart888888888888888');
//                 },
//                 onChangeEnd: (double) {
//                   print('9999999999999999onChangeEnd99999999999999999999');
//                 },
//                 onChanged: (double) {
//                   setState(() {
//                     progressValue = double.roundToDouble();
//                   });
//                 },
//               ),
//               width: 300.0,
//             ),
//             SliderTheme(
//               data: SliderTheme.of(context).copyWith(
// //                activeTickMarkColor:Colors.yellowAccent,
//                 activeTrackColor: Colors.yellowAccent,//实际进度的颜色
// //                inactiveTickMarkColor:Colors.black
//                 thumbColor: Colors.black,//滑块中心的颜色
//                 inactiveTrackColor:Colors.red,//默认进度条的颜色
//                 valueIndicatorColor: Colors.blue,//提示进度的气派的背景色
//                 valueIndicatorTextStyle: new TextStyle(//提示气泡里面文字的样式
//                   color: Colors.white,
//                 ),
//                 inactiveTickMarkColor:Colors.blue,//divisions对进度线分割后 断续线中间间隔的颜色
//                 overlayColor: Colors.pink,//滑块边缘颜色
//               ),
//               child: new Container(
//                 width: 340.0,
//                 margin: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0),
//                 child: new Row(
//                   children: <Widget>[
//                     new Text('0.0'),
//                     new Expanded(
//                       flex: 1,
//                         child: new Slider(
//                           value: progressValue,
//                           label: '$progressValue',
//                           divisions: 10,
//                           onChanged: (double){
//                             setState(() {
//                               progressValue=double.floorToDouble();//转化成double
//                             });
//                           },
//                           min: 0.0,
//                           max: 100.0,
//                         ),
//                     ),
//                     new Text('100.0'),
//                   ],
//                 ),
//               ),
//             ),

          ],
        ),
      ),
    );
  }

  void onViewPlayerCreated(viewPlayerController) async {
    _liveVideoController = viewPlayerController;
//    _liveVideoController
//        .setVideoURI("http://7o50kb.com2.z0.glb.qiniucdn.com/c1.1.mp4");
    // _liveVideoController.setLivePlay(userId: "e77rl86axa", channelId: "127889");
    _liveVideoController.setVid("ef4825bc7e585a9b7b5894ded7ded188_e");
    // Object args = await _liveVideoController.getChatHistory(channelId: "127889", userId: "e77rl86axa", begin: "0", end: "-1");
    // print("11111>>>> ${args.length}");
    // _liveVideoController.getChatRoomListUsers(channelId: "127889", userId: "e77rl86axa", page: "1", length: "100000");
    // _liveVideoController.initSocket(channelId: '127889', nickName: 'dp26726179', avatar: 'https://image.dapengjiaoyu.com/images/avatars/41avatar.jpg');
    _liveVideoController.listen((Map arg) async {
      print(arg['type']);
      
      switch (arg["type"]) {
        case "leftUp":
          int brightness = await _liveVideoController.getBrightness();
          brightness = brightness + 1;
          if (brightness > 100) {
            brightness = 100;
          }
          _liveVideoController.setBrightness(brightness);
          print('亮度{$brightness}');
          break;
        case "leftDown":
          int brightness = await _liveVideoController.getBrightness();
          brightness = brightness - 1;
          if (brightness < 0) {
            brightness = 0;
          }
          _liveVideoController.setBrightness(brightness);
          print('亮度{$brightness}');
          break;
        case "rightUp":
          int volume = await _liveVideoController.getVolume();
          print(volume);
          _liveVideoController.setVolume(volume + 1);
          print(await _liveVideoController.getVolume());
          break;
        case "rightDown":
          int volume = await _liveVideoController.getVolume();
          print(volume);
          _liveVideoController.setVolume(volume - 1);
          print(await _liveVideoController.getVolume());
          break;
        case "swipeLeft":
          print("======== swipeLeft ========");
          print(arg['start']);
          print(arg['end']);
          break;
        case "swipeRight":
          print("======== swipeLeft ========");
          print(arg['start']);
          print(arg['end']);
          break;
        case "doubleClick":
          // String msgType = arg["msgType"];
          // bool b = await _liveVideoController.isPlaying();
          // print(b?"YES":"NO");

          // _liveVideoController.seekTo(58919);

          // int i = await _liveVideoController.getCurrentPosition();
          // print("当前的播放节点是 " + i.toString());
          break;
      }
    });

    // _liveVideoController.setOnChatListener((Map arg) async {
    //   print("消息监听类型 " + arg['type']);
    //   switch (arg["type"]) {
    //     case "connect_status":
    //       break;
    //     case "receive_message":
    //       print("监听到双击事件了啊");
    //       break;
    //   }
    // });

    // _liveVideoController.setOnChatListener((Map map) {
    //   print("=======================>");
    //   print(map);
    //   print("=======================>");
    // });
  }
}
